export default class Util{
	// constructor(){
	// 	super()
	// }

	getMoviesData(...arg){
	 // let _self = this;
	 // console.log(arg);
	  let req = wx.request({
	    url: arg[0],
	    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
	    header: {
	      "Content-Type": "json"
	    },
	    success: function (res) {
	     // let util = new Util();
	     // util.processDoubanData(res.data,arg[1]);
	     console.log(res);
	     if(res.statusCode<200 && res.statusCode>=400){
	     	setTimeout(()=>{
	     		req();
	     	}, 2000)
	     }else{
	     	arg[2](res);
	     }
	    },
	    fail: function (error) {
	      // fail
	      console.log(error)
	    }
	  })
	}

	processDoubanData(...arg){
	    let moviesInfo = arg[0].subjects;
	    // console.log(arg);
	    let infos = [];
	    let readyData = {};
	  
	    moviesInfo.map((val,index,arr)=>{
	      let title = val.title.length>6?`${val.title.slice(0, 6)}...`:val.title;
	      let imagesUrl = val.images;
	      let average = val.rating.average;
	      let stars = val.rating.stars;
	      let id = val.id;
	      stars = new Array(5).fill(false).fill(true,0,stars.slice(0, 1));
	      let info = {
	        title,
	        imagesUrl,
	        average,
	        stars,
	        id
	      }
	      infos.push(info);
	    });

	    return {
	    	infos,
	        categoryTitle:arg[0].title,
	        key:arg[1]
	    };
	   
	}

	processMoiveData(arg){
		let data = arg.res;
		let title = data.title,
			countries = data.countries[0],
			year = data.year,
			imageUrl = data.images.large,
			originalTitle = data.original_title===title?title:title+'\x20'+data.original_title,
			average = data.rating.average,
			stars = data.rating.stars,
			director = data.directors[0].name,
			casts= data.casts,
			commentsCount = data.comments_count,
			wishCount = data.wish_count,
			genres = data.genres.join('/'),
			summary = data.summary;
		stars = new Array(5).fill(false).fill(true,0,stars.slice(0, 1));
		let roleName = [];
		let castInfos = [];
		casts.map(val=>{
			roleName.push(val.name);
			castInfos.push({imgUrl:val.avatars.large,name:val.name})
		})
		let roleNames = roleName.join('/');

		return{
			title,
			countries,
			year,
			imageUrl,
			originalTitle,
			average,
			stars,
			director,
			commentsCount,
			wishCount,
			genres,
			summary,
			roleNames,
			castInfos
		}

	}
	
}